import url from '@/api/url.js';
import Encr from '@/common/aes.js';


//app自动更新
export const getVersion=function (param){
	return new Promise((resolve,reject)=>{
		
		uni.request({
			url:url()+'/app/login/getVersion',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				res.data=JSON.parse(Encr.decrypt(res.data))
				resolve(res.data)
			},
			fail:(err)=>{
				console.log('fail',err)
				reject(err)
			}
		});
	})
}

/**
 *  参数
 * 查询数据参数格式{data:[{id:xx,...}]pageCount:10, // 页条数pageNo:1 //   页码}
 * 修改数据参数格式{id:xx,...}
 */

export const loginWx=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/login/App',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					console.log(res);  
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail',err)
				reject(err)
			}
		});
	})
	return promise;
};



//查询已签收列表页  List
export const SelectDeliveryTaskDetailList=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/Detail/SelectDeliveryTaskDetailList',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};



//查询对detail 进行评价
export const EditDeliveryTaskDetail=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/Detail/EditDeliveryTaskDetail',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};





//查询已签收详情 列表页  List
export const SelectDeliveryPackageList=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/Package/SelectDeliveryPackageList',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};






//查询 授权  列表页  List
export const SelectCustomerClerkList=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/Clerk/SelectCustomerClerkList',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};


//取消授权
export const CancelCustomerClerk=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/Clerk/CancelCustomerClerk',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};




//添加授权
export const AddlCustomerClerk=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/Clerk/AddlCustomerClerk',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else if(res.data.code=='264'){
					resolve(res.data);
				}else{
					reject(res.data);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};



//app端查询  扫码 验签 改状态
export const EditDeliveryPackageRow=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/PackageRow/EditDeliveryPackageRow',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else if(res.data.code=='264'){
					resolve(res.data);
				}else{
					reject(res.data);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};



//app端查询  扫码 验签 改状态
export const editDeliveryPackageRowType=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/PackageRow/editDeliveryPackageRowType',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else if(res.data.code=='264'){
					resolve(res.data);
				}else{
					reject(res.data);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};



//app端查询  扫码 验签 改状态
export const SelectDeliveryShopPackageList=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/Package/SelectDeliveryShopPackageList',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				console.log(JSON.stringify(res.data));
				if(res.data.code=='200'){
					resolve(res.data);
				}else if(res.data.code=='264'){
					resolve(res.data);
				}else{
					reject(res.data);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};






//app端查询  扫码 详情总包裹
export const SelectDeliveryPackageConut=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/Package/SelectDeliveryPackageConut',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else if(res.data.code=='264'){
					resolve(res.data);
				}else{
					reject(res.data);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};




//app端 更新定位
export const updateCarInfo=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/deliveryCarTrackHistory/updateCarInfo',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else if(res.data.code=='264'){
					resolve(res.data);
				}else{
					reject(res.data);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};




//ajax 距举例
//接口功能说明
export const login=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/login/login',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.Code=='200'){
					resolve(res.Map);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};


/**
 * 本地缓存
 * @param {Object} param
 */

export const catchDeliver = function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.showLoading({
		    title: '加载缓存'
		})
		let time = new Date().getTime()
		console.log(param,'加载缓存传参')
		console.log(Encr.encryptData(JSON.stringify(param)),'加载缓存传参加密')
		console.log(url() + '/app/DeliveryTask/cacheDeliverInfoByUserId')
		uni.request({
			url: url() + '/app/DeliveryTask/cacheDeliverInfoByUserId',
			method: 'POST',
			timeout:300000,
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {  
				console.log('加载缓存成功','time:'+(new Date().getTime()-time))
				console.log(res.data)
				// uni.showModal({
				// 	content:Encr.decrypt(res.data)
				// })
				res.data=JSON.parse(Encr.decrypt(res.data))
				console.log(res.data.code)
				if(res.data.code=='200'){
					//统一修改状态
					uni.setStorageSync('catchAll', res)
					let list = res.data.map.allShopInfo
					console.log(list.length)
					for (let i in list){
						// console.log(list[i].deliveryStatus,'装态')
						list[i].i = i
						// list[i].deliveryStatus = 1
					}
					uni.setStorageSync('catch', res)
					uni.setStorageSync('catchStatus', true)
					for (let i in list){
						list[i].packages = []
					}
					
					
					resolve(true);
				}else{
					reject(false);
				}
			},
			fail:(err)=>{
				console.log(err,'加载缓存')
				console.log('fail')
				reject(false)
			},
			complete(){
				uni.hideLoading();
			}
		});
	})
	return promise;
};

//配送页面数据查询
export const showdeliveryinfo=function (param){
	
	let ifFansh = false
	
	if (uni.getStorageSync('catch') !== ''){
		let allShopInfo = uni.getStorageSync('catch').data.map.allShopInfo
		
		for (let i in allShopInfo) {
			if (allShopInfo[i].deliveryStatus !== 6) {
				ifFansh = true
			}
		}
		
		if (ifFansh === false){
			uni.setStorageSync('catchStatus', false)
		}
		if (uni.getStorageSync('catchStatus')){
			// console.log('[读取数据缓存]', uni.getStorageSync('catch'))
			//查询状态是否完成
			return uni.getStorageSync('catch').data
		}
	}
	console.log('未读取缓存')
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/DeliveryTask/deliveryList',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {  
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};




//所有配送员数据查询
//接口功能说明
export const showAllDeliveryMan=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/DeliveryTask/allDeliveryMan',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {  
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};



//调配配送员
//接口功能说明
export const allocateDeliveryMan=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/DeliveryTask/allocateDeliveryMan',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {  
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};


//配送员点击开始配送
//接口功能说明
export const beginDelivery=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/DeliveryTask/beginDelivery',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {  
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};


//配送员点击查看店铺包裹详情
//接口功能说明
export const selectShopPackage=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/DeliveryTask/selectShopPackage',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {  
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};

//配送员点击开始配送
//接口功能说明
export const updateDeliverToday=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/DeliveryTask/updateDeliverToday',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {  
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};


//app端查询  通知公告
export const selectSysNotificationList=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/sysNotification/selectSysNotificationList',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else if(res.data.code=='264'){
					resolve(res.data);
				}else{
					reject(res.data);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};

//app端查询 数据字典
export const dictionaryList=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/PackageRow/dictionaryList',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {
				
				res.data=JSON.parse(Encr.decrypt(res.data))
				
				if(res.data.code=='200'){
					resolve(res.data);
				}else if(res.data.code=='264'){
					resolve(res.data);
				}else{
					reject(res.data);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};


		//同意调配
export const agreeAllocateMan=function (param){
    var promise = new Promise((resolve,reject)=>{
        uni.request({
            url:url()+'/app/DeliveryTask/agreeAllocateMan',
            method: 'POST',
            data: Encr.encryptData(JSON.stringify(param)),
            success: function(res) {  
                console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
                if(res.data.code=='200'){
                    resolve(res.data);
                }else{
                    reject(res);
                }
            },
            fail:(err)=>{
                console.log('fail')
                reject(err)
            }
        });
    })
    return promise;
};
//拒绝调配
export const disagreeAllocateMan=function (param){
    var promise = new Promise((resolve,reject)=>{
        uni.request({
            url:url()+'/app/DeliveryTask/disagreeAllocateMan',
            method: 'POST',
            data: Encr.encryptData(JSON.stringify(param)),
            success: function(res) {  
                console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
                if(res.data.code=='200'){
                    resolve(res.data);
                }else{
                    reject(res);
                }
            },
            fail:(err)=>{
                console.log('fail')
                reject(err)
            }
        });
    })
    return promise;
};

//查询人员车辆关系
export const selectDeliveryCarNum=function (param){
    var promise = new Promise((resolve,reject)=>{
        uni.request({
            url:url()+'/app/DeliveryTask/selectDeliveryCarNum',
            method: 'POST',
            data: Encr.encryptData(JSON.stringify(param)),
            success: function(res) {  
                console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
                if(res.data.code=='200'){
                    resolve(res.data);
                }else{
                    reject(res);
                }
            },
            fail:(err)=>{
                console.log('fail')
                reject(err)
            }
        });
    })
    return promise;
};


//查询二维码有效时间
export const getQRCodeTimeLimit=function (param){
    var promise = new Promise((resolve,reject)=>{
        uni.request({
            url:url()+'/app/Detail/getQRCodeTimeLimit',
            method: 'POST',
            data: Encr.encryptData(JSON.stringify(param)),
            success: function(res) {  
                console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
                if(res.data.code=='200'){
                    resolve(res.data);
                }else{
                    reject(res);
                }
            },
            fail:(err)=>{
                console.log('fail')
                reject(err)
            }
        });
    })
    return promise;
};



//查询负责人列表
export const getDeliveryTaskQueList=function (param){
    var promise = new Promise((resolve,reject)=>{
        uni.request({
            url:url()+'/app/Task/getDeliveryTaskQueList',
            method: 'POST',
            data: Encr.encryptData(JSON.stringify(param)),
            success: function(res) {  
                console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
                if(res.data.code=='200'){
                    resolve(res.data);
                }else{
                    reject(res);
                }
            },
            fail:(err)=>{
                console.log('fail')
                reject(err)
            }
        });
    })
    return promise;
};


//一键装车
export const updateDeliveryStatus=function (param){
	uni.showLoading({
	    title: '加载中'
	})
    var promise = new Promise((resolve,reject)=>{
        uni.request({
            url:url()+'/app/DeliveryTask/updateDeliveryStatus',
            method: 'POST',
            data: Encr.encryptData(JSON.stringify(param)),
            success: function(res) {  
               uni.hideLoading();
				res.data=JSON.parse(Encr.decrypt(res.data))
                if(res.data.code=='200'){
                    resolve(res.data);
                }else{
                    reject(res);
                }
            },
            fail:(err)=>{
                console.log('fail')
                reject(err)
				uni.hideLoading();
            }
        });
    })
    return promise;
};
//领导页展示
export const selectAllInfo=function (param){
    var promise = new Promise((resolve,reject)=>{
        uni.request({
            url:url()+'/app/DeliveryTask/selectAllInfo',
            method: 'POST',
            data: Encr.encryptData(JSON.stringify(param)),
            success: function(res) {  
                console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
                if(res.data.code=='200'){
                    resolve(res.data);
                }else{
                    reject(res);
                }
            },
            fail:(err)=>{
                console.log('fail')
                reject(err)
            }
        });
    })
    return promise;
};


//异常配送自行处理
export const updatestatus=function (param){
    var promise = new Promise((resolve,reject)=>{
        uni.request({
            url:url()+'/app/DeliveryTask/updatestatus',
            method: 'POST',
            data: Encr.encryptData(JSON.stringify(param)),
            success: function(res) {  
                console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
                if(res.data.code=='200'){
                    resolve(res.data);
                }else{
                    reject(res);
                }
            },
            fail:(err)=>{
                console.log('fail')
                reject(err)
            }
        });
    })
    return promise;
}; 


//配送员上传图片验签
//接口功能说明
export const uploadBaseImage=function (param){
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/file/user/upload',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {  
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};


//配送员确认验签
//接口功能说明
export const undateTaskImg=function (param){
	// console.log( param )
	var promise = new Promise((resolve,reject)=>{
		uni.request({
			url:url()+'/app/DeliveryTask/undateTaskImg',
			method: 'POST',
			data: Encr.encryptData(JSON.stringify(param)),
			success: function(res) {  
				console.log('success')
				res.data=JSON.parse(Encr.decrypt(res.data))
				if(res.data.code=='200'){
					resolve(res.data);
				}else{
					reject(res);
				}
			},
			fail:(err)=>{
				console.log('fail')
				reject(err)
			}
		});
	})
	return promise;
};

	/* 
	引用举例
	  
	import {login  } from '@/api/request.js';
	
	
	login(param).then(res => {
		if (res.code == 200) {
			uni.showToast({
				title:'成功'
			})
			
		} else{
			uni.showModal({
				title: 'False',
				content: 'code不等于200 失败',
				showCancel: false
			})
		}
	}).catch(err => {
		uni.showModal({
			title: 'False',
			content: '失败',
			showCancel: false
		})*/
	
	